package com.ccnu.demojava;

import com.ccnu.demojava.annotation.AnnotationUtil;
import com.ccnu.demojava.annotation.Apple;
import com.ccnu.demojava.jna.lhplLibrary;
import com.ccnu.demojava.math.RMath;
import com.ccnu.demojava.qtpmessage.QtpMessage;
import com.ccnu.demojava.qtpmessage.SocketClient;
import com.ccnu.demojava.reflect.RReflectBean;
import com.ccnu.demojava.reflect.RTestReflect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.math.BigDecimal;
import java.net.Socket;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Hello world!
 *
 */
public class RNormalApp {
	static Logger log = LoggerFactory.getLogger(RNormalApp.class);
	private static final Map<String,String> map_app_key = new HashMap<String, String>();
	private static final Map<String,String> map_from_channel = new HashMap<String, String>();
	private static final Map<String,String> map_secret_key = new HashMap<String, String>();
	static {
//		init();
	}
	
	private static void init() {
		Properties pos = new Properties();
		try {
			pos.load(new FileInputStream("D:/1"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Enumeration<?> posEnum = pos.propertyNames();
		while(posEnum.hasMoreElements()) {
			String prot = (String) posEnum.nextElement();		
			// 如果涵盖bank.channel
			if(prot.contains("bank.channel.")) {
				// 获取
				String protValue = pos.getProperty(prot);
				String bankId = prot.substring("bank.channel.".length());				
				String[] protValueArray = protValue.split(",");
				if(protValueArray.length != 3) {
					System.out.println("配置文件有误[" + prot + "]");
				}
				map_app_key.put(bankId, protValueArray[0]);
				map_from_channel.put(bankId, protValueArray[0]);
				map_secret_key.put(bankId, protValueArray[0]);
			}
		}
		System.out.println(map_app_key.toString());
	}
	
	
	// 测试主程序
	public static void main(String[] args) {
//		testAnnotation();
//		testFields();
//		testLambda();
//		testInnerClass();
//		new RTestJson().testJson();
//		RBaseType.testBaseType();
//		RHashMap.testHashMap();
//		RSort.testAlgorithm();
//		RTestProxy.test();
//		RTestLoader.test();
		
//		RBeanTest.test();
//		RResources.test();
		
//		RTestDeliv.test();
		
//		new RTrans().testTrans();

//		// 测试数学公式
//		RMath.test();
//		
		// JDBC的测试
//		RJDBC.test();
		// MyBatis的测试
//		RMyBatis.test();
//		RMySQL.testInsert();
		
		// 加密解密测试
//		RCryptTest.test();
		
//		// 并发编程测试
//		RThreadTest.testDaemon();
//		RThreadTest.testSync();
//		RThreadTest.testNotifyAll();
//		RCountDownLatch.test();
//		RReentrantLock.test();
//		RAtomic.test();
//		RThreadLocal.test();
//		RBlockQueue.test();
//		RExchanger.test();
//		RDelayQueue.test();
//		RTransferQueue.test();
//		RCyclicBarrier.test();		
		
//		// JNI测试
//		RNative.test();
		
//		// 测试JVM
//		RJVM.testJMap();
		
		// 测试Class文件加载
//		RTest_ClassParse.test();

		// redis测试
//		RTestRedis.test();

//		RFtpUtil.downLoad();

//		testJNA();

//		testNumber();

		testQtpMsg();
	}

	public static void testQtpMsg() {
		try {
			SocketClient sc = new SocketClient();
			sc.connect("127.0.0.1", 6001);
			// 组织请求报文
			String body = "hello world!";
			QtpMessage req = new QtpMessage();
			req.getMsgHead().setTopic(11);
			req.getMsgHead().setMsgtype(6001);
			byte c = '0';
			req.getMsgHead().setVersion(c);
			req.getMsgHead().setService(c);
			req.setData(body.getBytes());

			// 发送给服务端
			if (sc.isConnected()) {
				sc.send(req);
			}
			while (true) {
				if (sc.isConnected()) {
					QtpMessage message = sc.recv();
					if (message != null) {
						System.out.println("recv ans:" + new String(message.getData()));
						Thread.sleep(100);
						sc.send(req);
					}
					Thread.sleep(1000);
				} else {
					break;
				}
			}
			sc.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static void testAnnotation() {
		AnnotationUtil.getFruitInfo(Apple.class);
	}

	public static void testFields() {
		new RTestReflect().getFieldsInfo(new RReflectBean());
	}

//	public static void testLambda() {
//		RTestLambda.testLambda();
//	}
//	
//	public static void testInnerClass() {
//		RInnerClass.RTestInnerClass t =  new RInnerClass("raohaijun", "32").new RTestInnerClass();
//		t.toLog();
//	}
//	
//	public static void testProxy() {
//		RJavaFieldTest t = new RJavaFieldTest();
//		t.setsName("raoyuke");
//		RProxy p = new RProxy(t);
//	}

	public static void testNumber() {
		double d = 0.00026338898199;
		System.out.println(d);
		Double dd = d;
		System.out.println(dd);

		BigDecimal bdd = new BigDecimal(d);
		System.out.println(bdd.toPlainString());

		NumberFormat nf = NumberFormat.getInstance();
		nf.setGroupingUsed(false);
		System.out.println(nf.format(0.00026338898199));

		DecimalFormat df = new DecimalFormat( "0.00000000000000000000");
		System.out.println(df.format(0.00026338898199));
	}

	public static void testJNA() {
		String version = "";
		System.out.println("load the library" + lhplLibrary.INSTANCE);
		lhplLibrary.INSTANCE.lhpl_Version(version);
		System.out.println("version:" + version.toString());

		String cpOut = "";
		String input = "";
		lhplLibrary.INSTANCE.lhpl_Bond_AccruedInterest(cpOut, input);
		System.out.println("lhpl_Bond_AccruedInterest:" + cpOut.toString());
	}
}
